----------------------------------------------------------------------------------------------------------------------------------
--[[

    体力系统

    体力系数 和 体力值 是两回事


]]--
----------------------------------------------------------------------------------------------------------------------------------
--
    local function GetReplica(self)
        return self.inst.replica.hutao_com_physical_power or self.inst.replica._.hutao_com_physical_power
    end
    local function set_current(self, value)
        local replica = GetReplica(self)
        if replica then
            replica:SetCurrent(value)
        end
    end
    local function set_max(self,value)
        local replica = GetReplica(self)
        if replica then
            replica:SetMax(value)
        end
    end
    local function set_running(self,value)
        local replica = GetReplica(self)
        if replica then
            replica:SetRunning(value)
        end
    end
----------------------------------------------------------------------------------------------------------------------------------
local hutao_com_physical_power = Class(function(self, inst)
    self.inst = inst

    self.DataTable = {}
    self.TempTable = {}
    self._onload_fns = {}
    self._onsave_fns = {}


    -------------------------------------------------------
    --
        self.current = 0
        self.max = 100
        self:AddOnSaveFn(function(self)
            self:Set("current",self.current)
            self:Set("max",self.max)
        end)
        self:AddOnLoadFn(function(self)
            self.current = self:Get("current",0)/4*3 -- 加载的时候丢失一些。
            self.max = self:Get("max",100)
        end)
    -------------------------------------------------------
    -- 体力系数
        self.__modulus_table = {}
        self.__modulus_remove_event = function(temp_inst)
            self:RemoveModulus(temp_inst)
        end
    -------------------------------------------------------
    -- running
        self.is_running = false
    -------------------------------------------------------


end,
nil,
{
    current = set_current,
    max = set_max,
    is_running = set_running,
})
------------------------------------------------------------------------------------------------------------------------------
--- 体力系数
    function hutao_com_physical_power:AddModulus(temp_inst,value)
        if self.__modulus_table[temp_inst] ~= nil then
            self:RemoveModulus(temp_inst)
        end
        self.__modulus_table[temp_inst] = value
        temp_inst:ListenForEvent("onremove",self.__modulus_remove_event)
    end
    function hutao_com_physical_power:RemoveModulus(temp_inst)
        local new_table = {}
        for k,v in pairs(self.__modulus_table) do
            if k ~= temp_inst then
                new_table[k] = v
            end
        end
        self.__modulus_table = new_table
        temp_inst:RemoveEventCallback("onremove",self.__modulus_remove_event)
    end
    function hutao_com_physical_power:GetModulus()
        local ret = 0
        for k,v in pairs(self.__modulus_table) do
            if k and k:IsValid() then
                ret = ret + v
            end
        end
        return ret
    end
------------------------------------------------------------------------------------------------------------------------------
--- 体力值
    function hutao_com_physical_power:SetCurrent(value)
        local old = self.current
        self.current = math.clamp(value,0,self.max)
        -- if old ~= self.current then
        --     print("体力值变化",old,self.current)
        -- end
    end
    function hutao_com_physical_power:SetMax(value)
        self.max = math.clamp(value,0,9999)
        self.current = math.clamp(self.current,0,self.max)
    end
    function hutao_com_physical_power:DoDelta(delta)
        self:SetCurrent(self.current + delta)
    end
    function hutao_com_physical_power:GetCurrent()
        return self.current
    end
    function hutao_com_physical_power:GetMax()
        return self.max
    end
    function hutao_com_physical_power:GetPercent()
        return self.current / self.max
    end
    function hutao_com_physical_power:IsFull()
        return self.current >= self.max
    end
------------------------------------------------------------------------------------------------------------------------------
--- running
    function hutao_com_physical_power:SetRunning(value)
        self.is_running = value
    end
    function hutao_com_physical_power:IsRunning()
        return self.is_running
    end
------------------------------------------------------------------------------------------------------------------------------
----- onload/onsave 函数
    function hutao_com_physical_power:AddOnLoadFn(fn)
        if type(fn) == "function" then
            table.insert(self._onload_fns, fn)
        end
    end
    function hutao_com_physical_power:ActiveOnLoadFns()
        for k, temp_fn in pairs(self._onload_fns) do
            temp_fn(self)
        end
    end
    function hutao_com_physical_power:AddOnSaveFn(fn)
        if type(fn) == "function" then
            table.insert(self._onsave_fns, fn)
        end
    end
    function hutao_com_physical_power:ActiveOnSaveFns()
        for k, temp_fn in pairs(self._onsave_fns) do
            temp_fn(self)
        end
    end
------------------------------------------------------------------------------------------------------------------------------
----- 数据读取/储存

    function hutao_com_physical_power:Get(index,default)
        if index then
            return self.DataTable[index] or default
        end
        return nil or default
    end
    function hutao_com_physical_power:Set(index,theData)
        if index then
            self.DataTable[index] = theData
        end
    end

    function hutao_com_physical_power:Add(index,num,min,max)
        if index then
            if min and max then
                local ret = (self.DataTable[index] or 0) + ( num or 0 )
                ret = math.clamp(ret,min,max)
                self.DataTable[index] = ret
                return ret
            else
                self.DataTable[index] = (self.DataTable[index] or 0) + ( num or 0 )
                return self.DataTable[index]
            end
        end
        return 0
    end
------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------
    function hutao_com_physical_power:OnSave()
        self:ActiveOnSaveFns()
        local data =
        {
            DataTable = self.DataTable
        }
        return next(data) ~= nil and data or nil
    end

    function hutao_com_physical_power:OnLoad(data)
        if data.DataTable then
            self.DataTable = data.DataTable
        end
        self:ActiveOnLoadFns()
    end
------------------------------------------------------------------------------------------------------------------------------
return hutao_com_physical_power







